<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

class f_builder_TargetParser
{
    private $targets;

    /**
	 * @return f_builder_TargetParser
	 */
	private function __construct()
	{
		return $this;
	}

	/**
	 * Get instance
	 *
	 * @return f_builder_TargetParser
	 */
	public static function getInstance()
	{
		return new self();
	}

    public function buildTargets()
    {
        $xmlContent = $this->buildXml();

        $this->writeXml($xmlContent);
    }

    private function writeXml($xmlContent)
    {
        $f = fopen(WEBAPP_DIR.DS.'config'.DS.'targets.xml', 'w+');

        if($f)
        {
            if(CLI_MODE) echo COLOR_GREEN.'WRITING'.COLOR_NORMAL.' -> Writing file webapp/config/targets.xml'."\n";
            fwrite($f, $xmlContent);
            fclose($f);
            if(CLI_MODE) echo COLOR_GREEN.'WRITING'.COLOR_NORMAL.' -> Done !'."\n";
        }
    }

    private function buildXml()
    {
        $targets = $this->getAllTargetsFromXml();

        $check = array();

        if(count($targets) > 0)
        {
            $xml = new XMLWriter();
            $xml->openMemory();
            $xml->setIndent(true);
            $xml->setIndentString('    ');
            $xml->startDocument('1.0', 'UTF-8');
            $xml->startElement('targets');

            foreach($targets as $module => $values)
            {
                $xml->writeComment(' '.'modules_'.$module.' ');

                if(CLI_MODE) echo COLOR_GREEN.'COMPILE'.COLOR_NORMAL.' -> Adding targets from '.$module."\n";

                foreach($values as $target)
                {
                    if(!isset($check[$target['module']][$target['name']]))
                    {
                        $check[$target['module']][$target['name']] = true;

                        $xml->startElement('target');

                        $xml->writeAttribute('module', $target['module']);
                        $xml->writeAttribute('name', $target['name']);
                        $xml->writeAttribute('applyOn', $target['applyOn']);
                        $xml->writeAttribute('label', $target['label']);

                        if(isset($target['uniqueBy']))
                        {
                            $xml->writeAttribute('uniqueBy', $target['uniqueBy']);
                        }

                        $xml->endElement();
                    }
                }
            }
            
            $xml->endElement();
            $xml->endDocument();
            return $xml->outputMemory(true);
        }
    }

    private function getTargetsXmlFiles()
    {
        $modulesFiles = f_file_Resolver::getInstance()->fromDirectory(MODULES_DIR)->setRecursive(true)->setMask('config/targets.xml')->find();
        $webappFiles = f_file_Resolver::getInstance()->fromDirectory(WEBAPP_MODULES_DIR)->setRecursive(true)->setMask('config/targets.xml')->find();

        $result = array_merge($modulesFiles, $webappFiles);

        return $result;
    }

    private function getAllTargetsFromXml()
    {
        $files = $this->getTargetsXmlFiles();

        $result = array();

        if(count($files) > 0)
        {
            foreach($files as $file)
            {
                $elements = f_xml_Xpath::getInstance($file)->getElementsFromXpath('target');

                if(count($elements) > 0)
                {
                    foreach($elements as $element)
                    {
                        $result[f_file_Resolver::getModuleNameFromFilePath($file)][] = $element->disableI18n()->getAttributes();
                    }
                }
            }
        }

        return $result;
    }
}
